//
//  CNNewFeatureViewController.swift
//  Ccweibo
//
//  Created by 陈锦聪 on 16/3/24.
//  Copyright © 2016年 Coco. All rights reserved.
//

import UIKit
import SnapKit

private let reuseIdentifier = "CNNewFeatureCell"

class CNNewFeatureViewController: UICollectionViewController {
    
    let layout = UICollectionViewFlowLayout()
    // 父类没有这个方法,子类不需要override
    init() {
        super.init(collectionViewLayout: layout)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        
        prepareCollectionView()
    }
   
    private func prepareCollectionView() {
        
        layout.itemSize = UIScreen.mainScreen().bounds.size
        layout.minimumLineSpacing = 0
        layout.minimumInteritemSpacing = 0
        layout.scrollDirection = UICollectionViewScrollDirection.Horizontal
        collectionView?.pagingEnabled = true
        collectionView?.bounces = false
        collectionView?.registerClass(CNNewFeatureCell.self, forCellWithReuseIdentifier: reuseIdentifier)
    }

}
// MARK: - collectionViewDelegate
extension CNNewFeatureViewController {
    
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return newFeaturePageCount
    }
    
    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! CNNewFeatureCell
//        cell.backgroundColor = UIColor.randomColor()
        cell.index = indexPath.item
        return cell
    }
    
    override func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
        
        let page = Int(scrollView.contentOffset.x / UIScreen.mainScreen().bounds.width)
        if page == newFeaturePageCount - 1 {
            
            let cell = collectionView?.cellForItemAtIndexPath(NSIndexPath(forItem: page, inSection: 0)) as! CNNewFeatureCell
            cell.startButtonAnimation()
        }
    }
}

// MARK: - collectionViewCell
class CNNewFeatureCell: UICollectionViewCell {
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        prepareUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    var index: Int = 0 {
        didSet {
            startButton.hidden = true
            bgImageView.image = UIImage(named: "new_feature_\(index+1)")
        }
    }
    
    func startButtonAnimation() {
        
        startButton.hidden = false
        startButton.transform = CGAffineTransformMakeScale(0, 0)
        UIView.animateWithDuration(0.9, delay: 0.1, usingSpringWithDamping: 0.5, initialSpringVelocity: 5, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
            self.startButton.transform = CGAffineTransformIdentity
            }, completion: nil)
    }
    
    private func prepareUI() {
        
        contentView.addSubview(bgImageView)
        contentView.addSubview(startButton)
        
        bgImageView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(UIEdgeInsetsZero)
        }
        startButton.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(contentView.snp_centerX)
            make.bottom.equalTo(contentView).offset(-160)
        }
    }
    
    private lazy var bgImageView: UIImageView = UIImageView(image: UIImage(named: "new_feature_1"))
    
    private lazy var startButton: UIButton = {
        let button = UIButton()
        button.setTitle("开始体验", forState: UIControlState.Normal)
        button.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "new_feature_finish_button_highlighted"), forState: UIControlState.Highlighted)
        button.addTarget(self, action: "startButtonAction", forControlEvents: UIControlEvents.TouchUpInside)
        return button
    }()
    
    func startButtonAction() {
        
        AppDelegate.loadViewController(CNMainViewController())
    }
}
